package com.hrabun.goodmorningalarm;

import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

public class AlertsListAdapter extends SimpleAdapter  {
	
	private final Activity context;

	public AlertsListAdapter(Context aContext,
			List<? extends Map<String, ?>> data, int resource, String[] from,
			int[] to) {
		super(aContext, data, resource, from, to);
		this.context = (Activity) aContext;
		// TODO Auto-generated constructor stub
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		AlarmItem aItem;
		
		View rowView = convertView;
	
		if (rowView == null) {

            LayoutInflater inflater = context.getLayoutInflater();
            rowView = inflater.inflate(R.layout.alarm_item, null, true);
            holder = new ViewHolder();
            holder.txtAlarmTime = (TextView) rowView.findViewById(R.id.txtAlarmItemTime);
            holder.btnActive = (ToggleButton) rowView.findViewById(R.id.btnAlarmItemToggle);
            holder.btnSun = (ToggleButton) rowView.findViewById(R.id.btn_alarmItem_sun);
            holder.btnMon = (ToggleButton) rowView.findViewById(R.id.btn_alarmItem_mon);
            holder.btnTue = (ToggleButton) rowView.findViewById(R.id.btn_alarmItem_tue);
            holder.btnWed = (ToggleButton) rowView.findViewById(R.id.btn_alarmItem_wed);
            holder.btnThur = (ToggleButton) rowView.findViewById(R.id.btn_alarmItem_thur);
            holder.btnFri = (ToggleButton) rowView.findViewById(R.id.btn_alarmItem_fri);
            holder.btnSat = (ToggleButton) rowView.findViewById(R.id.btn_alarmItem_sat);
            rowView.setTag(holder);
            
            @SuppressWarnings("unchecked")
			Map<String, String> dataRow = (Map<String, String>) ((AdapterView<?>) parent).getItemAtPosition(position);
            aItem = new AlarmItem();
            aItem = aItem.getAlarmItemById(dataRow.get(Global.DATA_ID_ATTR));
            
            holder.btnActive.setTag(aItem);
            holder.txtAlarmTime.setTag(aItem);
            holder.btnActive.setChecked(aItem.active);
            holder.btnSun.setChecked(aItem.sunday);
            holder.btnMon.setChecked(aItem.monday);
            holder.btnTue.setChecked(aItem.tuesday);
            holder.btnWed.setChecked(aItem.wednesday);
            holder.btnThur.setChecked(aItem.thursday);
            holder.btnFri.setChecked(aItem.friday);
            holder.btnSat.setChecked(aItem.saturday);
            
			if (!Global.pref_isSundayStarting()) {
				LinearLayout lin = (LinearLayout) rowView.findViewById(R.id.layout_AlarmItemDays);
				lin.removeView(holder.btnSun);
				lin.addView(holder.btnSun);
			}
            
            setFontType(holder);
            setFocusable(holder);
		}

		holder = (ViewHolder) rowView.getTag();
        aItem = (AlarmItem) holder.btnActive.getTag();
        holder.txtAlarmTime.setText(Global.getTimeFromDate(aItem.alarmTime));
        holder.btnActive.setChecked(aItem.active);
        
        holder.txtAlarmTime.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				AlarmItem item = (AlarmItem) v.getTag();
				Toast.makeText(context, "Click. Item id= " +item.id, Toast.LENGTH_LONG).show();
				((MainActivity) context).changeAlarmItem(item);
				
			}
		});
        
  
        holder.btnActive.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				AlarmItem item = (AlarmItem) buttonView.getTag();
				Toast.makeText(context, "Check. Item id= " +item.id, Toast.LENGTH_LONG).show();
				if (isChecked) {
					item.activate();
				} else {
					item.deactivate();
				}
				
			}
		});
		
		return rowView;
	}
	
	private void setFocusable(ViewHolder holder) {
		holder.txtAlarmTime.setFocusable(false);
		holder.btnSun.setFocusable(false);
		holder.txtAlarmTime.setLongClickable(true);
		
	}
	
	private void setFontType(ViewHolder holder) {
        holder.txtAlarmTime.setTypeface(Global.setFontRibeye());
        holder.btnActive.setTypeface(Global.setFontRoboto());
        
        holder.btnSun.setTypeface(Global.setFontRoboto());
        holder.btnMon.setTypeface(Global.setFontRoboto());
        holder.btnTue.setTypeface(Global.setFontRoboto());
        holder.btnWed.setTypeface(Global.setFontRoboto());
        holder.btnThur.setTypeface(Global.setFontRoboto());
        holder.btnFri.setTypeface(Global.setFontRoboto());
        holder.btnSat.setTypeface(Global.setFontRoboto());
	}
	
//	static class ViewHolder {
//        public TextView txtAlarmTime;
//        public ToggleButton btnActive;
//        public ToggleButton btnSun;
//        public ToggleButton btnMon;
//        public ToggleButton btnTue;
//        public ToggleButton btnWed;
//        public ToggleButton btnThur;
//        public ToggleButton btnFri;
//        public ToggleButton btnSat;
//    }
	
	
}
